Hello and welcome back to our case study.
So in this case study we want to apply our skills and a bit larger case actually and we will apply random
number generation back to arrest operations and the so-called method chaining.
And also we want to compare the NUM pain and the pace and standard library in terms of performance length
of code readability.
Yeah and actually how intuitive the respective code might be for others.
So but now let's start and let's assume the following situation.
So we want to create 100 random into just between 1 and 10 and then even count how many times we get
the integer one.
So on average I would expect that ten times out of 100.
And if you do this only one time the simulation the result can be quite different to what we expect
and to check if our expectation that we get 10 times the integer one we have to simulate this many times
so let's say ten thousand times to get a statistically reliable result and then be similar 10000 times
and then we calculate the mean of all the outcomes are all simulations and then we should be very close
to to 10 on average at least I hope actually.
All right so let's start and we import an umpire and this is already the whole number code.
But I would like to derive the code.
So first of all we want to create 100 random integers in the range one till eleven.
So we get here 100 integers and then we check which of them are equal to 1.
So then we should get a ending array with the boolean objects so you can see false through false false
and then we want to calculate how many times you have to be in the job on and therefore we just sum
up our array.
So let's remember that a true stands for the value of one and a false sense for the value zeros.
So if you sum up the boolean array we should get to the number of occurrences of the enter to 1 and
here we have 12.
So our expectation was 10 and here only with one simulation.
Yet because it's random we got twelve.
And now let's repeat this process ten thousand times so we create 10000 times a hundred random integers
so in total of 1 million integers and then we reshape this 1 million integers to a matrix of 10000 rows
and 100 columns then for each integer we check whether it's equal to 1 or not.
And then we have to sum over all rows how many troops or how many times we have the occurrence of the
integer 1.
So we have to take Xs so equals 1.
So we sum over each row.
We have 10000 simulations now and if we execute this year then we can see now we get 10000 results so
we simulated this process 10000 times.
And in our first simulation we got 12 times the Indians are 1 9 and the second one nine times.
And now we also get six times four times so this match.
Uh yeah statistical variability or actually.
And yeah if you want to check whether our expectation is true that we on average expect 10 occurrences
of the integer one we have to take the mean of all the results and then I can see we are very close
to 10.
So now we created the many random numbers we use to vector us operations and also the so-called method
chaining so we changed one method after another.
So we started to create a random integer then we reshape that.
Then we took the sum of our troops for each row and then we took the mean of all results.
So this is called Method chaining and we can also measure the time that this process or this code takes
on average this time.
Function
that takes up a time so we have one million random integers so we got here 60 milliseconds.
And now what we want to do now we actually want to simulate the same process with the pace and standard
library and therefore we have to impart the random module.
And yes actually the code that does the same job as 0 0 an umpire code with Python standard library
instrument.
So let's start here with the inner for loop.
So a hundred times we are creating a random integer in the range between 1 and 10.
And then we check for each created random integer.
If it's equal to 1 and if it's 1 We append a true to our list.
Edit And if it's not equal to 1 We append files to our list.
And then we calculate the sum of all of our truisms and false in our list and append this to a resides
list and indeed resides.
Lists are all reserved for in total ten thousand simulations.
I put this whole code here into a function called simulation.
So that makes it easier later yet to measure the time actually.
So there's no deeper reason for it.
So they can execute the Sally and then we can run our function and simulate that space in standard library
and I can see we got a nine point nine.
Also here we are very close to 10.
Now let's measure the time of the performance of the code and compare it to an umpire.
We have here one point two seconds.
So this code took us one point two seconds and an umpire did actually the same job in six in milliseconds.
Some umpires 100 times faster.
And that's actually a huge difference.
And you can also see here we have a very condensed code so we have actually only one line of code here.
And in contrast with sort of the pace the standard library we have here nested for loop and if statements.
However some people say that here the readability of the code is much easier.
And also it's what you'd have to understand.
So at least if you have done two or three times the nested blue band some statements it might be quite
easy to understand what the code is doing here in comparison the vector as campaign code and method
chaining might be a bit less intuitive at least if you are not used to it.
All right.
So this is it for the time being and in the next session we will have a look at summary statistics so
help to see there by.